﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;

namespace BattleThis.Core
{
	/// <summary>
	/// Service to load or save games.
	/// </summary>
	public class GameService
	{

		#region Variablen
		private static MapService MapService = new MapService();
		private static List<Game> SavedGame = CreateMockGames();
		public static int GameIdCounter = 0;
		#endregion

		#region Properties

		/// <summary>
		/// Gets or sets the unit service.
		/// </summary>
		/// <value>
		/// The unit service.
		/// </value>
		private UnitService UnitService { get; set; }

		#endregion

		#region Methoden

		/// <summary>
		/// Initializes a new instance of the <see cref="GameService"/> class.
		/// </summary>
		public GameService()
		{
			this.UnitService = new UnitService();
		}

		private static List<Game> CreateMockGames()
		{
			var mocklist = new List<Game>();

			var map1 = MapService.LoadMap( "Sample1" );
			var reference1 = new GameReference( 1, "Game1" );
			var player1 = new Player( new Guid( "75DC9F13-8515-45B1-8028-0518F8233B58" ), "Player A" );
			var player2 = new Player( new Guid( "31C13E03-1C23-49CE-A521-91D5B794E3A2" ), "Player B" );
			player1.Color = Color.Red;
			player2.Color = Color.Blue;
			mocklist.Add( new Game( reference1, map1, new Player[] { player1, player2 } ) );

			var map2 = MapService.LoadMap( "Sample2" );
			var reference2 = new GameReference( 2, "Game1" );
			var player3 = new Player( new Guid( "75DC9F13-8515-45B1-8028-0518F8233B58" ), "Player A" );
			var player4 = new Player( new Guid( "31C13E03-1C23-49CE-A521-91D5B794E3A2" ), "Player B" );
			player3.Color = Color.Red;
			player4.Color = Color.Blue;
			mocklist.Add( new Game(reference2,map2, new Player[] { player3, player4 } ) );

			return new List<Game>( mocklist );

		}

		/// <summary>
		/// Creates the specified name.
		/// </summary>
		/// <param name="name">The name.</param>
		/// <param name="mapName">Name of the map.</param>
		public void Create( string name, string mapName )
		{
			var map = MapService.LoadMap( mapName );
			var reference = new GameReference( GameIdCounter++, name );
			var game = new Game( reference, map );

			SavedGame.Add( game );
		}

		/// <summary>
		/// Loads all Games of the current User.
		/// </summary>
		/// <returns></returns>
		public IEnumerable<Game> Load()
		{
			return GameService.SavedGame;
		}

		/// <summary>
		/// Loads the game specified by id.
		/// </summary>
		/// <param name="id">The id.</param>
		/// <returns></returns>
		public Game Load( int id )
		{
			var game = SavedGame.Where( x=> x.Id == id).Single();
			this.UnitService.SetUnits( game );

			return game;
		}

		/// <summary>
		/// Saves the specified game.
		/// </summary>
		/// <param name="game">The game.</param>
		public void Save( Game game )
		{
			if( game == null )throw new ArgumentNullException( "game" );
			throw new NotImplementedException();
		}

		/// <summary>
		/// Closes the specified game.
		/// </summary>
		/// <param name="game">The game.</param>
		public void Close( Game game )
		{
			if( game == null )throw new ArgumentNullException( "game" );

			SavedGame = null;
		}

		#endregion

	}
}